<?php
namespace app\common\model;

use org\util\TreeUtil;
use think\Db;

class AreaModel extends BaseModel
{
    protected $name = 'area'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    //添加地区
    public function addData($data)
    {
        $this->isUpdate(false)->allowField(true)->save($data);
        $data   = $this->select()->toArray();
        $parent = TreeUtil::parentChannel($data, $this->getAttr('id'));
        $parent = array_reverse($parent); //反转数组
        $path   = [];
        foreach ($parent as $value) {
            $path[] = $value['area_name'];
        }
        $edata['level'] = count($parent);
        $edata['path']  = implode(',', $path);
        $this->where('id', $this->getAttr('id'))->update($edata);
        return true;
    }
    /*读取器*/
    /*修改器*/
    /**
     * 同步更新级联数据
     * @return bool
     */
    public function createJs()
    {
        $area = Db::table('area')->order('area_sort')->select();
        $tree = TreeUtil::channelLevel($area,0);
        $result = [];
        $i=0;
        foreach ($tree as $onevalue){
            $result[$i] = ['id'=>$onevalue['id'],'name'=>$onevalue['area_name']];
            $j=0;
            if(empty($onevalue['children'])){
                $result[$i]['cityList'] = [];
            }
            foreach ($onevalue['children'] as $twokey=>$twovalue){
                $result[$i]['cityList'][$j] = ['id'=>$twovalue['id'],'name'=>$twovalue['area_name']];
                $k=0;
                if(empty($twovalue['children'])){
                    $result[$i]['cityList'][$j]['areaList'] = [];
                }
                foreach ($twovalue['children'] as $threekey=>$threevalue){
                    $result[$i]['cityList'][$j]['areaList'][$k] = ['id'=>$threevalue['id'],'name'=>$threevalue['area_name']];
                    $k+=1;
                }
                $j+=1;
            }
            $i+=1;
        }
        $filePath  = 'public/plugins/jsAddress/';
        $provList  = json_encode($result, JSON_UNESCAPED_UNICODE);
        $jsContent = file_get_contents($filePath . 'tpl.js');
        $jsContent = str_replace('[\'888999\']', $provList, $jsContent);
        file_put_contents($filePath . 'jsAddress.js', $jsContent);
        return true;
    }
    //生成cityselect联动数据
    public function buildCityPicker() {
        $area = Db::table('area')->order('area_sort')->select();
        $area = TreeUtil::channelLevel($area,0);
        $dist = array();
        $i=0;
        foreach($area as $one){
            $dist[$i]['name'] = $one['area_name'];
            $dist[$i]['code'] = $one['id'];
            $j=1;
            $dist[$i]['sub'][0] = array('name'=>'请选择');
            foreach($one['children'] as $two){
                $dist[$i]['sub'][$j] = array('name'=>$two['area_name'],'code'=>$two['id']);
                if(empty($two['children'])){
                    $dist[$i]['type'] = 0;
                }else{
                    $dist[$i]['sub'][$j]['sub'][] = array('name'=>'请选择');
                    foreach($two['children'] as $three){
                        $dist[$i]['sub'][$j]['sub'][] = array('name'=>$three['area_name'],'code'=>$three['id']);
                    }
                    $dist[$i]['type'] = 1;
                }
                $j+=1;
            }
            $i+=1;
        }
        $json = json_encode($dist, true);
        $tpl_path = "Public/home/bus/js/city-tpl.js";
        $js_content = file_get_contents($tpl_path);
        $js_content = str_replace('8080',$json,$js_content);
        file_put_contents('Public/home/bus/js/city-picker.js', $js_content);
        return true;
    }

    /**
     * 更新缓存
     * @return bool
     */
    public function updateCache()
    {
        $list = Db::table('area')->order('area_sort')->select(); //避免走模型读取器
        foreach ($list as $v) {
            $data[$v['id']] = $v['area_name'];
        }
        cache('area', $data);
        return true;
    }
}